<?php
require_once 'models/model_manager.php';
require_once 'models/zone_geographique.php';

class zone_geographique_manager extends Manager
{	
	public function get_by_id($i) {
		$query = "SELECT * FROM zone_geographique WHERE idZoneGeo = " . $i;
		$result = oci_parse($this->_db, $query);
		oci_execute($result);
		$data = oci_fetch_assoc($result);
		$zone = new zone_geographique($data);
		return $zone;		
	}
	
	public function get_all() {
		$zones = array();
		$query = "SELECT * FROM zone_geographique";
		$result = oci_parse($this->_db, $query);
		oci_execute($result);
		
		while ($data = oci_fetch_assoc($result)) {
			$zones[] = new zone_geographique($data);
		}

		return $zones;
	}

	public function get_all_occ($entite) {
		$zones = array();
		$query = "SELECT * FROM zone_occupee WHERE idEntite = " . $entite;
		$result = oci_parse($this->_db, $query);
		oci_execute($result);
		
		while ($data = oci_fetch_assoc($result)) {
			$zone = $this->get_by_id($data['IDZONEGEO']);
			$zone->set_idzoneocc($data['IDZONEOCC']);
			$zone->set_debut($this->get_date_debut_occ($data['IDZONEOCC']));
			$zone->set_fin($this->get_date_fin_occ($data['IDZONEOCC']));
			$zones[] = $zone;
		}

		return $zones;
	}

	public function get_date_debut_occ($id) {
		$query =
			"SELECT idDate, reference, to_char(\"DATE\", 'DD/MM/SYYYY') AS \"DATE\" FROM date_historique
			WHERE reference = 1
				AND idDate IN (SELECT idDate FROM debut_occupation
								WHERE idZoneOcc = " . $id . ")";
		$result = oci_parse($this->_db, $query);
		
		oci_execute($result);
		$d = oci_fetch_assoc($result);
		return new date_historique($d);
	}

	public function get_date_fin_occ($id) {
		$query =
			"SELECT idDate, reference, to_char(\"DATE\", 'DD/MM/SYYYY') AS \"DATE\" FROM date_historique
			WHERE reference = 1
				AND idDate IN (SELECT idDate FROM fin_occupation
								WHERE idZoneOcc = " . $id . ")";
		$result = oci_parse($this->_db, $query);
		
		oci_execute($result);
		$d = oci_fetch_assoc($result);
		return new date_historique($d);
	}
	
	public function max_id() {
		$query = "SELECT max(idZoneGeo) FROM zone_geographique";
		$result = oci_parse($this->_db, $query);
		oci_execute($result);
		$r = oci_fetch_row($result);
		return $r[0];
	}
	
	public function max_occ_id() {
		$query = "SELECT max(idZoneOcc) FROM zone_occupee";
		$result = oci_parse($this->_db, $query);
		oci_execute($result);
		$r = oci_fetch_row($result);
		return $r[0];
	}
	
	public function add($e) {
		$query = "INSERT INTO zone_geographique(idZoneGeo, codeISO, wikiZoneGeo, nomZoneGeo, longitude, latitude)
			VALUES (" . $e->get_idzonegeo() . ",'" . $e->get_codeiso() . "','" . $e->get_wikizonegeo() . "','" . $e->get_nomzonegeo() . "'," . $e->get_longitude() . "," . $e->get_latitude() . ")";
		$result = oci_parse($this->_db, $query);
		oci_execute($result);
	}

	public function delete($i) {
		$query = "DELETE FROM zone_geographique WHERE idZoneGeo = " . $i;
		$result = oci_parse($this->_db, $query);
		oci_execute($result);
	}
}
?>
